package com.masterclient.app.httpservice;

import android.util.Log;

import com.eoner.managerlibrary.netsign.NetEnvManager;
import com.masterclient.app.base.MessageCallBack;
import com.masterclient.app.modules.search.HotSearchBean;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * 作者：sean
 * 创建时间：2017/6/22 15:55
 * 功能说明：
 */

public class ApiService {

    /**
     * 热搜
     */
    public void getHotSearchList(MessageCallBack<HotSearchBean.DataBean> callBack){
        Observable observable = RetrofitUtil.getInstance().getHotSearchList(NetEnvManager.BASE_SURL+"1/search/hot_search_word.html");
        toSubscribe(observable, new Observer<HotSearchBean>() {
            @Override
            public void onSubscribe(@NonNull Disposable d) {
                Log.d("tag", "onSubscribe: ");
            }

            @Override
            public void onNext(@NonNull HotSearchBean bean) {
                if(0==bean.getCode()){
                    callBack.onSuccess(bean.getData());
                }
            }

            @Override
            public void onError(@NonNull Throwable e) {
                e.printStackTrace();
            }

            @Override
            public void onComplete() {
                Log.d("tag", "onComplete: ");
            }
        });
    }

    //添加线程管理并订阅
    private void toSubscribe(Observable o, Observer observer){
        o.subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }
}
